package _15_竞赛练习;

public class _2017_网格游戏 {

    public static void main(String[] args) {
        int[][] ins = new int[][]{
                {2, 5, 4},
                {1, 5, 1},
        };
        _2017_网格游戏 v = new _2017_网格游戏();
        System.out.println(v.gridGame(ins));
    }

    // 一趟遍历
    public long gridGame(int[][] grid) {
        int len = grid[0].length;
        // 首先计算，第一行的总大小
        long row1Sum = 0;
        long row2Sum = 0;
        long sum = Long.MAX_VALUE;
        for (int i = 0; i < len; i++) {
            row1Sum += grid[0][i];
        }
        for (int i = 0; i < len; i++) {
            row1Sum -= grid[0][i];
            if (i > 0) {
                row2Sum += grid[1][i - 1];
            }
            // 计算二者最小值
            sum = Math.min(sum, Math.max(row1Sum, row2Sum));
            if (row1Sum < row2Sum) break;
        }
        return sum;
    }

}
